package com.xuan.judge.callback;

import cn.hutool.core.util.StrUtil;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import com.github.dockerjava.core.command.ExecStartResultCallback;
import com.xuan.common.core.enums.CodeRunStatus;
import lombok.Getter;
import lombok.Setter;


@Setter
@Getter
public class DockerStartResultCallback extends ExecStartResultCallback {

    private CodeRunStatus codeRunStatus;  //记录执行成功还是失败

    private String errorMessage;

    private String message;

    @Override
    public void onNext(Frame frame) {
        StreamType streamType = frame.getStreamType();
        String payload = new String(frame.getPayload());
        System.out.println("onNext called with StreamType: " + streamType);
        System.out.println("onNext called with payload: " + payload);

        if (StreamType.STDERR.equals(streamType)) {
            if (StrUtil.isEmpty(errorMessage)) {
                errorMessage = payload;
            } else {
                errorMessage += payload;
            }
            codeRunStatus = CodeRunStatus.FAILED;
        } else {
            message = payload;
            if (!CodeRunStatus.FAILED.equals(codeRunStatus)) {
                codeRunStatus = CodeRunStatus.SUCCESS;
            }
        }
        super.onNext(frame);
    }
}
